import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';

///请求权限
Future<bool> requestPermissions(Permission permission, String text,
    {bool showAlert = true, GestureTapCallback? cancelTap}) async {
  final status = await permission.status;
  if (status != PermissionStatus.granted) {
    final Map<Permission, PermissionStatus> statuses =
        await [permission].request();
    if (!(statuses[permission] == PermissionStatus.granted)) {
      if (showAlert) {
        // alertSureCancel('该服务需要开启$text授权',
        //     sureTap: () => openAppSettings(), cancelTap: cancelTap);
      } else {
        openAppSettings();
      }
    }
    return statuses[permission] == PermissionStatus.granted;
  }
  return true;
}

///根据需求格式化时间戳
String timeStampTrans(int timeStamp, {int? type}) {
  final dateTime = DateTime.fromMillisecondsSinceEpoch(timeStamp);

  if (type == 0) {
    ///转换 为日期
    /// 2020-11-12
    return dateTime.toString().substring(0, 10);
  } else if (type == 1) {
    return dateTime.toString().substring(0, 19);
  } else if (type == 2) {
    ///转换 为时分
    /// 10：10
    return dateTime.toString().substring(11, 19);
  } else if (type == 3) {
    ///转换为时分秒
    ///10:10:10
    return dateTime.toString().substring(11, 19);
  }

  /// 2020-11-12 12:12
  final times = dateTime.toString().split(':');
  return times[0] + ':' + times[1];
}

//设置状态栏是否亮色显示
void setStatusBarLight(bool isLight) {
  const Color color = Colors.transparent;
  if (isLight is bool) {
    SystemChrome.setSystemUIOverlayStyle(isLight
        ? const SystemUiOverlayStyle(
            systemNavigationBarColor: Colors.black54,
            systemNavigationBarDividerColor: Colors.transparent,
            statusBarColor: color,
            systemNavigationBarIconBrightness: Brightness.light,
            statusBarIconBrightness: Brightness.light,
            statusBarBrightness: Brightness.dark)
        : const SystemUiOverlayStyle(
            systemNavigationBarColor: Colors.black54,
            systemNavigationBarDividerColor: color,
            statusBarColor: color,
            systemNavigationBarIconBrightness: Brightness.light,
            statusBarIconBrightness: Brightness.dark,
            statusBarBrightness: Brightness.light));
  }
}

///页面跳转
void push(BuildContext context, Widget widget) {
  Navigator.push(context, MaterialPageRoute(builder: (context) => widget));
  //Navigator.of(context).popUntil((route) => false);//删除所有路由
}

///跳转并关闭当前页面
void pushReplace(BuildContext context, Widget widget) {
  Navigator.pushAndRemoveUntil(
    context,
    MaterialPageRoute(builder: (context) => widget),
    (route) => route == null,
  );
}
